﻿using System.Drawing.Drawing2D;
using DFGK.IService.BBS;
using DFGK.Service.BBS;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models.BBS;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;

namespace DFGK_WMS.WebAPI.Controllers.Platform
{
    /// <summary>
    /// 共享文件
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShareFileInfoController : BaseController
    {
        private readonly IShareFileService _shareFileService;

        public ShareFileInfoController(IShareFileService shareFileService, IWebHostEnvironment webEnvironment) : base(webEnvironment)
        {
            _shareFileService = shareFileService;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="title">文件标题</param>
        /// <param name="pageInex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPageData(string? title, int pageInex=1, int pageSize=50)
        {
            int totalCount = 0;
            var pageData = _shareFileService.GetShareFileInfoPageData(title, out totalCount, pageInex, pageSize);
            return Ok(BaseResult.Result(pageData, "查询成功！", "200", totalCount));
        }

        /// <summary>
        /// iD查询
        /// </summary>
        /// <param name="shareFileId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShareFile(long shareFileId)
        {
            var shareInfo = _shareFileService.GetShareFileInfo(shareFileId);
            return Ok(BaseResult.Result(shareInfo, "查询成功！", "200", 0));
        }

        /// <summary>
        /// 新增/修改文件共享信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitShareFile(ShareFileInfo model)
        {
            if (model.Id > 0)
            {
                model.ModifyDate = DateTime.Now;
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
            }
            else
            {
                model.CreateUserName = base.BaseGetNowUserName();
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateDate = DateTime.Now;


            }
            bool result = _shareFileService.SubmitShareFileData(model, out string message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 删除文件共享信息
        /// </summary>
        /// <param name="shareFileId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelShareFile(long shareFileId)
        {
            var result = _shareFileService.DelShareInfo(shareFileId);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadFile(IFormFile file)
        {
            return await BaseUploadFile(file);
        }
    }
}
